<?php
class Model_User extends Df_Model
{

    public function __construct ($id = null) {
        parent::__construct(new Model_DbTable_Users(), $id);
    }

    public function getAllUsers ($page) {
       return $this->_dbTable->fetchAll(
			$this->_dbTable->select ()
			->limitPage($page, 50)
			->order('id DESC')
		);
    }

    //fill form with data from DB
    public function populateForm () {
        return $this->_row->toArray();
    }

    //	public function findUserByEmail($user_email){
    //		return $this->_dbTable->fetchRow($this->_dbTable->select()->where('email = ?',$user_email));
    //	}
    public function authorize ($login, $password, $remember = false) {
        $auth = Zend_Auth::getInstance();
        $authAdapter = new Zend_Auth_Adapter_DbTable(
        Zend_Db_Table::getDefaultAdapter(), 'users', 'login', 'password', 
        'md5(?)');
        $authAdapter->setIdentity($login)->setCredential($password);
        $result = $auth->authenticate($authAdapter);
        if ($result->isValid()) {
            $storage = $auth->getStorage();
            $storage->write(
            $authAdapter->getResultRowObject(null, array('password')));
            //remember for one day
            if ($remember) {
                Zend_Session::rememberMe(60 * 60 * 24);
            }
            return true;
        }
        return false;
    }

    public function getUserInfoByLogin ($login) {
        $user_db = new Model_DbTable_Users();
        $user_model = $user_db->fetchRow(
        $user_db->select()
            ->where('login = ?', $login));
        return $user_model;
    }

    public function checkOnline ($user_id) {
        $timestamp = $this->_dbTable->fetchRow(
        
        $this->_dbTable->select()
            ->where('id = ?', $user_id));
        $min = round((time() - $timestamp->lastonline) / 60);
        
        if ($min < 6)
            return true;
        return false;
    }

    //write user activity to DB, to check that user is online. Need to do every time
    public function setActivity () {
        $this->_row->lastonline = time();
        $this->_row->save();
    }

}
?>